package org.glut.wechat.addresslist.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.glut.wechat.addresslist.common.Result;
import org.glut.wechat.addresslist.entity.User;
import org.glut.wechat.addresslist.service.IUserService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; // 🔥 必须导入！

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userService;

    // 搜索用户
    @GetMapping("/search")
    public Result<User> searchUser(@RequestParam String keyword) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", keyword) // 账号匹配
                .or()
                .eq("phone", keyword);  // 手机号匹配

        User user = userService.getOne(wrapper);

        if (user != null) {
            return Result.success(user);
        } else {
            return Result.fail("该用户不存在");
        }
    }

    /**
     * 获取单个用户信息1
     */
    @GetMapping("/{id}")
    public Result<Map<String, Object>> getUserInfo(
            @PathVariable Integer id,
            @RequestHeader("Authorization") String token
    ) {
        User user = userService.getById(id);
        if (user == null) {
            return Result.fail("用户不存在");
        }
        // 过滤密码等敏感字段
        Map<String, Object> userInfo = new HashMap<>();
        userInfo.put("id", user.getId());
        userInfo.put("username", user.getUsername());
        userInfo.put("phone", user.getPhone());
        userInfo.put("gender", user.getGender());
        userInfo.put("birthday", user.getBirthday());
        userInfo.put("wechatNumber", user.getWechatNumber());
        userInfo.put("signature", user.getSignature());
        userInfo.put("region", user.getRegion());
        userInfo.put("avatar", user.getAvatar());

        return Result.success(userInfo);
    }

    /**
     * 获取所有用户信息（过滤敏感字段）
     */
    @GetMapping("/users")
    public Result<List<Map<String, Object>>> getUsers(
            @RequestHeader("Authorization") String token
    ) {
        List<User> users = userService.list(new QueryWrapper<>());
        List<Map<String, Object>> userInfos = users.stream()
                .map(user -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", user.getId());
                    map.put("username", user.getUsername());
                    map.put("phone", user.getPhone());
                    map.put("gender", user.getGender());
                    map.put("birthday", user.getBirthday());
                    map.put("wechatNumber", user.getWechatNumber());
                    map.put("signature", user.getSignature());
                    map.put("region", user.getRegion());
                    map.put("avatar", user.getAvatar());
                    return map;
                })
                .collect(Collectors.toList());

        return Result.success(userInfos);
    }
}